# src/modules/barValue.py
import time

import pyautogui

from src.core.template_matcher import TemplateMatcher


class BarValue(TemplateMatcher):
    def __init__(self, gameWindow, config: dict):
        # config 包含: region_x, region_y, region_w, region_h, template_dir
        super().__init__(template_dir=config['template_dir'])
        self.region_x = gameWindow.x + config['region_x']
        self.region_y = gameWindow.y + config['region_y']
        self.region_w = config['region_w']
        self.region_h = config['region_h']
        self.digit_regions = config['digit_regions']

    def get_value(self):

        screenshot = pyautogui.screenshot(region=(self.region_x, self.region_y, self.region_w, self.region_h))

        # 假设分三块识别数字
        digits = []

        for region in self.digit_regions:
            x_start, x_end = region
            crop = screenshot.crop((x_start, 0, x_end, self.region_h))
            digit = self.match_digit(crop)
            if digit == -1:
                break
            digits.append(str(digit))

        return int(''.join(digits)) if digits != [] else 0